package net.affenkakka.shop;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;


public class CloudManager 
{
	private static String SHOP_KEY = "4DBE4709AE7E777C67A79777";
	private static Namespace ns = Namespace.getNamespace("http://www.cs.au.dk/dWebTek/2011");
	private static String cloudURL = "http://services.brics.dk/java4/cloud";
	
	public static Document login(String customerName, String customerPass)
	{
		String url = "/login";
		
		// Create XML document
		Document xmlDoc = new Document();
		
		Element root = new Element("login");
		root.setNamespace(ns);
		
		root.addContent(new Element("customerName").setNamespace(ns).addContent(customerName));
		
		root.addContent(new Element("customerPass").setNamespace(ns).addContent(customerPass));
		
		xmlDoc.setRootElement(root);
		
		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	public static Document sellItems(String itemID, String customerID, String saleAmount)
	{
		String url = "/sellItems";
		
		// Create XML document
		Document xmlDoc = new Document();
				
		Element root = new Element("sellItems");
		root.setNamespace(ns);
				
		root.addContent(new Element("shopKey").setNamespace(ns).addContent(SHOP_KEY));
		
		root.addContent(new Element("itemID").setNamespace(ns).addContent(itemID));
		
		root.addContent(new Element("customerID").setNamespace(ns).addContent(customerID));
		
		root.addContent(new Element("saleAmount").setNamespace(ns).addContent(saleAmount));
				
		xmlDoc.setRootElement(root);

		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	public static Document createItem(String itemName)
	{
		String url = "/createItem";
		
		// Create XML document
		Document xmlDoc = new Document();
						
		Element root = new Element("createItem");
		root.setNamespace(ns);
						
		root.addContent(new Element("shopKey").setNamespace(ns).addContent(SHOP_KEY));
				
		root.addContent(new Element("itemName").setNamespace(ns).addContent(itemName));
						
		xmlDoc.setRootElement(root);

		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	public static Document createCustomer(String customerName, String customerPass)
	{
		String url = "/createCustomer";
		
		// Create XML document
		Document xmlDoc = new Document();
								
		Element root = new Element("createCustomer");
		root.setNamespace(ns);
								
		root.addContent(new Element("shopKey").setNamespace(ns).addContent(SHOP_KEY));
						
		root.addContent(new Element("customerName").setNamespace(ns).addContent(customerName));
		
		root.addContent(new Element("customerPass").setNamespace(ns).addContent(customerPass));
								
		xmlDoc.setRootElement(root);
				
		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	public static Document modifyItem(String itemID, String itemName, String itemPrice, String itemURL, String itemDescription)
	{
		String url = "/modifyItem";
		
		// Create XML document
		Document xmlDoc = new Document();
										
		Element root = new Element("modifyItem");
		root.setNamespace(ns);
										
		root.addContent(new Element("shopKey").setNamespace(ns).addContent(SHOP_KEY));
		
		root.addContent(new Element("itemID").setNamespace(ns).addContent(itemID));
								
		root.addContent(new Element("itemName").setNamespace(ns).addContent(itemName));
				
		root.addContent(new Element("itemPrice").setNamespace(ns).addContent(itemPrice));
		
		root.addContent(new Element("itemURL").setNamespace(ns).addContent(itemURL));

		root.addContent(new Element("itemDescription").setNamespace(ns).addContent(itemDescription));
										
		xmlDoc.setRootElement(root);

		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	public static Document adjustItemStock(String itemID, String adjustment)
	{
		String url = "/adjustItemStock";
		
		// Create XML document
		Document xmlDoc = new Document();
												
		Element root = new Element("modifyItem");
		root.setNamespace(ns);
												
		root.addContent(new Element("shopKey").setNamespace(ns).addContent(SHOP_KEY));
				
		root.addContent(new Element("itemID").setNamespace(ns).addContent(itemID));
										
		root.addContent(new Element("adjustment").setNamespace(ns).addContent(adjustment));
												
		xmlDoc.setRootElement(root);
		
		// Send request to cloud
		return sendToCloud(xmlDoc, url);
	}
	
	private static Document sendToCloud(Document xmlDoc, String url)
	{
		try 
		{
			// Setup URL
			URL cloud = new URL(cloudURL + url);
			
			// Connect to cloud
			HttpURLConnection connection = (HttpURLConnection) cloud.openConnection();
			
			// Create XMLOutputter
			XMLOutputter xmlOutputter = new XMLOutputter();
			
			if (xmlDoc != null)
			{
				connection.setDoOutput(true);
				OutputStreamWriter osWriter = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
				xmlOutputter.output(xmlDoc, osWriter);
				osWriter.close();
			}
			
			// Get response from cloud
			InputStream responseStream = connection.getInputStream();
			if (responseStream.available() != 0)
			{
				Document response = new SAXBuilder().build(responseStream);
				return response;
			}
		}
		
		catch (JDOMException e)
		{
			e.printStackTrace();
		}
		
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	public static List<Element> getItems()
	{
		try 
		{
			// Get and return shop items as a list
			Document itemXML = new SAXBuilder().build("http://services.brics.dk/java4/cloud/listItems?shopID=76");
			@SuppressWarnings("unchecked")
			List<Element> items = itemXML.getRootElement().getChildren();
			return items;
		} 
		
		catch (JDOMException e) 
		{
			e.printStackTrace();
		} 
		
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return null;
	}
}
